package org.gl.training.gwt.client.view;

import org.gl.training.gwt.client.service.UserService;
import org.gl.training.gwt.client.service.UserServiceAsync;
import org.gl.training.gwt.client.utils.MyAsyncCallback;
import org.gl.training.gwt.shared.model.UserVO;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormErrorOrientation;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.form.validator.MatchesFieldValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class RegisterView extends Window {

	private UserServiceAsync userService = GWT.create(UserService.class);

	private TextItem userNameItem;

	private TextItem fullNameItem;

	private TextItem emailItem;

	private PasswordItem passwordItem;

	private PasswordItem confirmPasswordItem;

	private TextItem notesItem;

	private DynamicForm form;

	public RegisterView() {
		super();
		setMembersMargin(15);
		setWidth(350);
		setHeight(250);
		setTitle("Register view");
		setShowMinimizeButton(false);
		setShowCloseButton(false);
		setIsModal(true);
		//setShowModalMask(true);
		centerInPage();

		VLayout layout = new VLayout(10);

		form = new DynamicForm();
		form.setErrorOrientation(FormErrorOrientation.RIGHT);
		form.setNumCols(3);
		form.setColWidths("54%", "5%", "41%");
		form.setLayoutAlign(Alignment.LEFT);

		userNameItem = new TextItem();
		userNameItem.setTitle("User Name*");
		userNameItem.setRequired(true);

		fullNameItem = new TextItem();
		fullNameItem.setTitle("Full Name*");
		fullNameItem.setRequired(true);

		emailItem = new TextItem();
		emailItem.setTitle("Email*");
		emailItem.setRequired(true);
		RegExpValidator emailValidator = new RegExpValidator();
		emailValidator.setExpression(".+@.+");
		emailValidator.setErrorMessage("invalid email address");
		emailItem.setValidators(emailValidator);

		MatchesFieldValidator passwordMatchValidator = new MatchesFieldValidator();
		passwordMatchValidator.setOtherField("passwordItem");
		passwordMatchValidator.setErrorMessage("Passwords do not match");

		passwordItem = new PasswordItem();
		passwordItem.setName("passwordItem");
		passwordItem.setTitle("Password*");
		passwordItem.setRequired(true);
		passwordItem.setType("password");
		LengthRangeValidator passwordLengthValidator = new LengthRangeValidator();
		passwordLengthValidator.setMin(new Integer(6));
		passwordItem.setValidators(passwordLengthValidator);

		confirmPasswordItem = new PasswordItem();
		confirmPasswordItem.setTitle("Confirm Password*");
		confirmPasswordItem.setRequired(true);
		confirmPasswordItem.setType("password");
		confirmPasswordItem.setValidators(passwordMatchValidator);

		notesItem = new TextItem();
		notesItem.setTitle("Notes");

		form.setFields(new FormItem[] { userNameItem, fullNameItem, emailItem,
				passwordItem, confirmPasswordItem, notesItem });

		HLayout hLayout = new HLayout(10);

		Button registerButton = new Button("Register");
		registerButton.addClickHandler(new RegisterHandler());

		Button cancelButton = new Button("Cancel");
		cancelButton.addClickHandler(new CancelHandler());

		hLayout.addMember(registerButton);
		hLayout.addMember(cancelButton);
		hLayout.setAlign(Alignment.CENTER);

		layout.addMember(form);
		layout.addMember(hLayout);

		addItem(layout);

	}

	private class RegisterCallback extends MyAsyncCallback<Void> {

		@Override
		public void onSuccess(Void result) {
			hide();

			LoginView loginView = new LoginView();
			loginView.show();

			showSuccessMessage("User registered successfuly");
		}
	}

	private class RegisterHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				String username = userNameItem.getValueAsString();
				String fullname = fullNameItem.getValueAsString();
				String email = emailItem.getValueAsString();
				String password = passwordItem.getValueAsString();
				String notes = notesItem.getValueAsString();

				UserVO userVO = new UserVO();
				userVO.setUserName(username);
				userVO.setFullName(fullname);
				userVO.setEmail(email);
				userVO.setPassword(password);
				userVO.setNotes(notes);
				
				userService.saveUser(userVO, new RegisterCallback());

			}
		}

	}

	private class CancelHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			hide();

			LoginView loginView = new LoginView();
			loginView.show();
		}
	}
}